import { mockRequestClient as defHttp } from '#/api/request';
import type {
  Resource,
  ResourceQueryParams,
  ResourceCreateParams,
  ResourceUpdateParams,
} from './types';
import type { PricingModel, PricingModelParams } from './pricing';
import type {
  CapabilityTemplate,
  CapabilityMarketItem,
  BindCapabilityParam,
} from './capability';
import type {
  SaaSApplication,
  IndustryTemplate,
  BuildApplicationParam,
} from './saas-factory';

// 资源注册中心API
export const ResourceRegistryService = {
  // 查询资源列表
  query: (params: ResourceQueryParams) => {
    return defHttp.get<{
      code: number;
      data: {
        items: Resource[];
        total: number;
        page: number;
        pageSize: number;
      };
      msg: string;
    }>({
      url: '/api/resource-center/resources',
      params,
    });
  },

  // 获取资源详情
  getDetail: (id: string) => {
    return defHttp.get<{
      success: boolean;
      data: Resource;
      message: string;
    }>({
      url: '/api/resource/registry/detail',
      params: { id },
    });
  },

  // 注册资源
  register: (data: ResourceCreateParams) => {
    return defHttp.post<{
      success: boolean;
      resourceId: string;
      message: string;
      errors: any[];
    }>({
      url: '/api/resource/registry/register',
      data,
    });
  },

  // 更新资源
  update: (data: ResourceUpdateParams) => {
    return defHttp.put<{
      success: boolean;
      message: string;
    }>({
      url: '/api/resource/registry/update',
      data,
    });
  },

  // 删除资源
  delete: (params: { id: string }) => {
    return defHttp.delete<{
      success: boolean;
      message: string;
    }>({
      url: '/api/resource/registry/delete',
      params,
    });
  },

  // 获取资源统计
  getStatistics: () => {
    return defHttp.get<{
      success: boolean;
      data: {
        totalResources: number;
        resourcesByType: Record<string, number>;
        resourcesByCategory: Record<string, number>;
        resourcesByStatus: Record<string, number>;
        recentlyCreated: Resource[];
        recentlyUpdated: Resource[];
      };
      message: string;
    }>({
      url: '/api/resource/registry/statistics',
    });
  },
};

// 定价配置中心API
export const ResourcePricingService = {
  // 获取定价模型列表
  getPricingModels: (params: any) => {
    return defHttp.get<{
      success: boolean;
      data: {
        list: PricingModel[];
        pagination: {
          current: number;
          pageSize: number;
          total: number;
          totalPages: number;
        };
      };
      message: string;
    }>({
      url: '/api/resource/pricing/models',
      params,
    });
  },

  // 创建定价模型
  createPricingModel: (data: PricingModelParams) => {
    return defHttp.post<{
      success: boolean;
      data: PricingModel;
      message: string;
    }>({
      url: '/api/resource/pricing/models',
      data,
    });
  },

  // 更新定价模型
  updatePricingModel: (data: PricingModelParams & { id: string }) => {
    return defHttp.put<{
      success: boolean;
      message: string;
    }>({
      url: '/api/resource/pricing/models',
      data,
    });
  },

  // 删除定价模型
  deletePricingModel: (params: { id: string }) => {
    return defHttp.delete<{
      success: boolean;
      message: string;
    }>({
      url: '/api/resource/pricing/models',
      params,
    });
  },

  // 计算价格
  calculatePrice: (data: any) => {
    return defHttp.post<{
      success: boolean;
      data: {
        customerPrice: number;
        platformPrice: number;
        breakdown: any;
      };
      message: string;
    }>({
      url: '/api/resource/pricing/calculate',
      data,
    });
  },
};

// 能力配置中心API
export const ResourceCapabilityService = {
  // 获取能力模板列表
  getTemplates: (params: any) => {
    return defHttp.get<{
      success: boolean;
      data: {
        list: CapabilityTemplate[];
        pagination: {
          current: number;
          pageSize: number;
          total: number;
          totalPages: number;
        };
      };
      message: string;
    }>({
      url: '/api/resource/capability/templates',
      params,
    });
  },

  // 获取能力市场列表
  getMarketItems: (params: any) => {
    return defHttp.get<{
      success: boolean;
      data: {
        list: CapabilityMarketItem[];
        pagination: {
          current: number;
          pageSize: number;
          total: number;
          totalPages: number;
        };
      };
      message: string;
    }>({
      url: '/api/resource/capability/market',
      params,
    });
  },

  // 绑定能力
  bindCapability: (data: BindCapabilityParam) => {
    return defHttp.post<{
      success: boolean;
      data: {
        id: string;
        resourceId: string;
        capabilityId: string;
        status: string;
        config: any;
        createdAt: string;
        bindingLog: any[];
      };
      message: string;
    }>({
      url: '/api/resource/capability/bind',
      data,
    });
  },

  // 获取已绑定能力列表
  getBoundCapabilities: (params: any) => {
    return defHttp.get<{
      success: boolean;
      data: {
        list: any[];
        pagination: {
          current: number;
          pageSize: number;
          total: number;
          totalPages: number;
        };
      };
      message: string;
    }>({
      url: '/api/resource/capability/bound',
      params,
    });
  },

  // 切换能力状态
  toggle: (resourceId: string, capabilityId: string, enabled: boolean) => {
    return defHttp.put<{
      success: boolean;
      message: string;
    }>({
      url: '/api/resource/capability/toggle',
      data: { resourceId, capabilityId, enabled },
    });
  },

  // 解绑能力
  unbind: (resourceId: string, capabilityId: string) => {
    return defHttp.delete<{
      success: boolean;
      message: string;
    }>({
      url: '/api/resource/capability/unbind',
      params: { resourceId, capabilityId },
    });
  },
};

// SaaS工厂API
export const SaaSFactoryService = {
  // 获取SaaS应用列表
  getApplications: (params: any) => {
    return defHttp.get<{
      success: boolean;
      data: {
        list: SaaSApplication[];
        pagination: {
          current: number;
          pageSize: number;
          total: number;
          totalPages: number;
        };
      };
      message: string;
    }>({
      url: '/api/resource/saas-factory/applications',
      params,
    });
  },

  // 获取业态模板列表
  getTemplates: (params: any) => {
    return defHttp.get<{
      success: boolean;
      data: {
        list: IndustryTemplate[];
        pagination: {
          current: number;
          pageSize: number;
          total: number;
          totalPages: number;
        };
      };
      message: string;
    }>({
      url: '/api/resource/saas-factory/templates',
      params,
    });
  },

  // 构建应用
  buildApplication: (data: BuildApplicationParam) => {
    return defHttp.post<{
      success: boolean;
      data: {
        id: string;
        name: string;
        description: string;
        industryType: string;
        templateId: string;
        status: string;
        progress: number;
        estimatedTime: string;
        createdAt: string;
        buildLog: any[];
      };
      message: string;
    }>({
      url: '/api/resource/saas-factory/build',
      data,
    });
  },

  // 部署应用
  deployApplication: (data: {
    applicationId: string;
    environment?: string;
    region?: string;
  }) => {
    return defHttp.post<{
      success: boolean;
      data: {
        id: string;
        applicationId: string;
        environment: string;
        region: string;
        status: string;
        progress: number;
        estimatedTime: string;
        startedAt: string;
        deploymentLog: any[];
        resources: any;
      };
      message: string;
    }>({
      url: '/api/resource/saas-factory/deploy',
      data,
    });
  },

  // 删除应用
  deleteApplication: (data: { applicationId: string }) => {
    return defHttp.post<{
      success: boolean;
      data: {
        applicationId: string;
        status: string;
        deletedAt: string;
        cleanupLog: any[];
      };
      message: string;
    }>({
      url: '/api/resource/saas-factory/delete',
      data,
    });
  },
};
